﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM
{
    public partial class VINNumberPopup : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["ItemCode"] != null && Request.QueryString["ItemCode"].Length > 0)
                {
                    hdnItemCode.Value = Request.QueryString["ItemCode"];
                    hdnWhsCode.Value = Request.QueryString["WhsCode"];
                    hdnNo.Value = Request.QueryString["DocLineNum"];
                    LoadOSRI();
                    LoadDefault();
                }
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                //GridViewRow selectedRow = this.grdBelong.SelectedRow;
                //if (selectedRow == null) return;
                /*Dictionary<string, string> dicBP = new Dictionary<string, string>();
                Label code = (Label)selectedRow.FindControl("lblCardCode");
                Label Name = (Label)selectedRow.FindControl("lblCardName");
                dicBP.Add(code.Text, Name.Text);*/

                /*Dictionary<string, string> dicBP = new Dictionary<string, string>();
                for (int i = 0; i < this.grdBP.Rows.Count; i++)
                {
                    CheckBox checkBox = (CheckBox)this.grdBP.Rows[i].FindControl("chkChild");

                    if (checkBox != null)
                    {
                        if (checkBox.Checked)
                        {
                            Label code = (Label)this.grdBP.Rows[i].FindControl("lblCardCode");
                            Label Name = (Label)this.grdBP.Rows[i].FindControl("lblCardName");
                            dicBP.Add(code.Text, Name.Text);
                        }
                    }
                }*/
                //Session["ChosenOSRI"] = selectedRow;

                DataTable dtSRNT = (DataTable)Session["tblSRNT"];
                if (dtSRNT == null)
                {
                    DataTable tblBelong = (DataTable)Session["OSRIBELONGTable"];
                    if (tblBelong != null && tblBelong.Rows.Count > 0)
                    {
                        foreach (DataRow row in tblBelong.Rows)
                            row["No"] = hdnNo.Value;
                    }

                    Session["tblSRNT"] = tblBelong;
                }
                else // Update tblSRNT datatable
                {
                    DataRow[] rows = dtSRNT.Select("No=" + hdnNo.Value);
                    if (rows.Length > 0)
                    {
                        foreach (DataRow row in rows)
                            dtSRNT.Rows.Remove(row);
                    }

                    DataTable tblBelong =  (DataTable)Session["OSRIBELONGTable"];
                    if (tblBelong != null && tblBelong.Rows.Count > 0)
                    {
                        foreach (DataRow dr in tblBelong.Rows)
                        {
                            DataRow row = dtSRNT.NewRow();
                            row["No"] = hdnNo.Value;
                            row["ItemCode"] = dr["ItemCode"];
                            row["SysNumber"] = dr["SysNumber"];
                            row["DistNumber"] = dr["DistNumber"];
                            row["InDate"] = dr["InDate"];
                            row["Quantity"] = dr["Quantity"];
                            row["WhsCode"] = dr["WhsCode"];
                            row["DocType"] = dr["DocType"];
                            row["DocEntry"] = dr["DocEntry"];
                            row["DocLineNum"] = hdnNo.Value;
                            row["Notes"] = dr["Notes"];
                            row["U_KeyNo"] = dr["U_KeyNo"];
                            dtSRNT.Rows.Add(row);
                        }
                    }
                    Session["tblSRNT"] = dtSRNT;
                }
                Session["ItemNo"] = hdnNo.Value;
                Session["OSRIBELONGTable"] = null;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectOSRI');", true);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBelong_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = this.grdBelong.SelectedRow;
                DataTable tblOSRIAVAI = (DataTable)Session["OSRIBELONGTable"];

                if (tblOSRIAVAI != null)
                {
                    int index = row.DataItemIndex;
                    var data = tblOSRIAVAI.Rows[index];
                    if (data != null)
                    {
                        Session["ChosenIntrSerial"] = data["IntrSerial"];
                        Session["ChosenInDate"] = data["InDate"];
                        Session["ChosenNotes"] = data["Notes"];
                        Session["ChosenSysSerial"] = data["SysSerial"];
                        Session["ChosenSysSerial"] = data["SysSerial"];
                        Session["ChosenItemCode"] = data["ItemCode"];
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

      

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBelong_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                    e.Row.Attributes["style"] = "cursor:pointer";
                    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdBelong, "Select$" + e.Row.RowIndex);
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdAvailable_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                    e.Row.Attributes["style"] = "cursor:pointer";
                    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdAvailable, "Select$" + e.Row.RowIndex);
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void grdBelong_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DataTable tblOSRI = (DataTable)Session["OSRIBELONGTable"];
            this.grdAvailable.PageIndex = e.NewPageIndex;
            BindBelong(tblOSRI);
        }

        protected void grdAvailable_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DataTable tblOSRI = (DataTable)Session["OSRIAVAITable"];
            this.grdAvailable.PageIndex = e.NewPageIndex;
            BindAvailable(tblOSRI);
        }

        private void LoadOSRI()
        {
            try
            {
                MasterData obj = new MasterData();
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;

                // Get VIN Number Available
                DataSet dsOSRI = obj.GetSerialNumbers(dbType, Session[Utils.AppConstants.ID_Dealer].ToString(), hdnItemCode.Value, hdnWhsCode.Value);
                if (dsOSRI != null && dsOSRI.Tables != null & dsOSRI.Tables[0].Rows.Count > 0)
                {
                    BindAvailable(dsOSRI.Tables[0]);
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindAvailable(DataTable tblOSRI)
        {
            Session["OSRIAVAITable"] = tblOSRI;
            this.grdAvailable.DataSource = tblOSRI;
            this.grdAvailable.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindBelong(DataTable tblOSRI)
        {
            Session["OSRIBELONGTable"] = tblOSRI;
            this.grdBelong.DataSource = tblOSRI;
            this.grdBelong.DataBind();
        }

        private void LoadDefault()
        {
            DataTable dtSRNT = (DataTable)Session["tblSRNT"];
            DataTable dtBelongVINNumber = new DataTable("SRNT");
            dtBelongVINNumber.Columns.Add("No");
            dtBelongVINNumber.Columns.Add("ItemCode");
            dtBelongVINNumber.Columns.Add("SysNumber");    // Default
            dtBelongVINNumber.Columns.Add("DistNumber");   // VIN Number
            dtBelongVINNumber.Columns.Add("InDate");       // Get default
            dtBelongVINNumber.Columns.Add("Quantity");     // 1
            dtBelongVINNumber.Columns.Add("WhsCode");      // Default
            dtBelongVINNumber.Columns.Add("DocType");      // 13
            dtBelongVINNumber.Columns.Add("DocEntry");     // Get from Incoming Payment No.
            dtBelongVINNumber.Columns.Add("DocLineNum");   // 1
            dtBelongVINNumber.Columns.Add("Notes");   // 
            dtBelongVINNumber.Columns.Add("U_KeyNo");

            if (dtSRNT != null && dtSRNT.Rows.Count > 0)
            {
                DataRow[] rows = dtSRNT.Select("ItemCode='" + hdnItemCode.Value + "' and WhsCode='" + hdnWhsCode.Value + "' and No=" + hdnNo.Value);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        DataRow dr = dtBelongVINNumber.NewRow();
                        dr["No"] = hdnNo.Value;
                        dr["ItemCode"] = row["ItemCode"];
                        dr["SysNumber"] = row["SysNumber"];
                        dr["DistNumber"] = row["DistNumber"];
                        dr["InDate"] = row["InDate"];
                        dr["Quantity"] = row["Quantity"];
                        dr["WhsCode"] = row["WhsCode"];
                        dr["DocType"] = row["DocType"];
                        dr["DocEntry"] = row["DocEntry"];
                        dr["DocLineNum"] = row["DocLineNum"];
                        dr["Notes"] = row["Notes"];
                        dr["U_KeyNo"] = row["U_KeyNo"];
                        dtBelongVINNumber.Rows.Add(dr);
                    }
                }
            }
            BindBelong(dtBelongVINNumber);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dtBelongVINNumber = (DataTable)Session["OSRIBELONGTable"];
            if (dtBelongVINNumber == null)
            {
                dtBelongVINNumber = new DataTable("SRNT");
                dtBelongVINNumber.Columns.Add("No");
                dtBelongVINNumber.Columns.Add("ItemCode");
                dtBelongVINNumber.Columns.Add("SysNumber");    // Default
                dtBelongVINNumber.Columns.Add("DistNumber");   // VIN Number
                dtBelongVINNumber.Columns.Add("InDate");       // Get default
                dtBelongVINNumber.Columns.Add("Quantity");     // 1
                dtBelongVINNumber.Columns.Add("WhsCode");      // Default
                dtBelongVINNumber.Columns.Add("DocType");      // 13
                dtBelongVINNumber.Columns.Add("DocEntry");     // Get from Incoming Payment No.
                dtBelongVINNumber.Columns.Add("DocLineNum");   // 1
                dtBelongVINNumber.Columns.Add("Notes");   //   
                dtBelongVINNumber.Columns.Add("U_KeyNo");
            }

            GridViewRow selectedRow = this.grdAvailable.SelectedRow;
            if (selectedRow != null)
            {
                DataRow dr = dtBelongVINNumber.NewRow();
                dr["No"] = hdnNo.Value;
                dr["ItemCode"] = ((Label)selectedRow.FindControl("lblItemCode")).Text;
                dr["SysNumber"] = ((Label)selectedRow.FindControl("lblSysSerial")).Text;
                dr["DistNumber"] = ((Label)selectedRow.FindControl("lblIntrSerial")).Text;
                dr["InDate"] = ((Label)selectedRow.FindControl("lblInDate")).Text;
                dr["Quantity"] = 1;//selectedRow.FindControl("Quantity");
                dr["WhsCode"] = hdnWhsCode.Value;//selectedRow.FindControl("WhsCode");
                //dr["DocType"] = selectedRow.FindControl("DocType");
                //dr["DocEntry"] = selectedRow.FindControl("DocEntry");
                //dr["DocLineNum"] = selectedRow.FindControl("DocLineNum");
                dr["Notes"] = ((Label)selectedRow.FindControl("lblNotes")).Text;
                dr["U_KeyNo"] = ((HiddenField)selectedRow.FindControl("hdnKeyNo")).Value;
                dtBelongVINNumber.Rows.Add(dr);

                BindBelong(dtBelongVINNumber);
            }

            DataTable tblOSRIAvailable = (DataTable)Session["OSRIAVAITable"];
            DataRow[] rows = tblOSRIAvailable.Select("IntrSerial='" + ((Label)selectedRow.FindControl("lblIntrSerial")).Text + "'");
            if (rows.Length > 0) tblOSRIAvailable.Rows.Remove(rows[0]);
            BindAvailable(tblOSRIAvailable);
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            DataTable dtAvailableVINNumber = (DataTable)Session["OSRIAVAITable"];
            if (dtAvailableVINNumber == null)
            {
                dtAvailableVINNumber = new DataTable("OSRI");
                dtAvailableVINNumber.Columns.Add("ItemCode");
                dtAvailableVINNumber.Columns.Add("SysSerial");    // Default
                dtAvailableVINNumber.Columns.Add("IntrSerial");   // VIN Number
                dtAvailableVINNumber.Columns.Add("InDate");       // Get default
                dtAvailableVINNumber.Columns.Add("Notes");   // 
                dtAvailableVINNumber.Columns.Add("U_KeyNo");
            }

            GridViewRow selectedRow = this.grdBelong.SelectedRow;
            if (selectedRow != null)
            {
                DataRow dr = dtAvailableVINNumber.NewRow();
                dr["ItemCode"] = ((Label)selectedRow.FindControl("lblItemCode")).Text;
                dr["SysSerial"] = ((Label)selectedRow.FindControl("lblSysNumber")).Text;
                dr["IntrSerial"] = ((Label)selectedRow.FindControl("lblDistNumber")).Text;

                dr["InDate"] = Utils.AppConstants.ConvertToDate(((Label)selectedRow.FindControl("lblInDate")).Text);
                dr["WhsCode"] = hdnWhsCode.Value;
                dr["Notes"] = ((Label)selectedRow.FindControl("lblNotes")).Text;
                dr["U_KeyNo"] = ((HiddenField)selectedRow.FindControl("hdnKeyNo")).Value;
                dtAvailableVINNumber.Rows.Add(dr);
                BindAvailable(dtAvailableVINNumber);
            }

            DataTable tblOSRIBelong = (DataTable)Session["OSRIBELONGTable"];
            DataRow[] rows = tblOSRIBelong.Select("DistNumber='" + ((Label)selectedRow.FindControl("lblDistNumber")).Text + "'");
            if (rows.Length > 0) tblOSRIBelong.Rows.Remove(rows[0]);
            BindBelong(tblOSRIBelong);

        }

        protected void grdAvailable_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

      

        protected void grdAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = this.grdBelong.SelectedRow;
                DataTable tblOSRIAVAI = (DataTable)Session["OSRIAVAITable"];

                if (tblOSRIAVAI != null)
                {
                    int index = row.DataItemIndex;
                    var data = tblOSRIAVAI.Rows[index];
                    if (data != null)
                    {
                        Session["ChosenIntrSerial"] = data["IntrSerial"];
                        Session["ChosenInDate"] = data["InDate"];
                        Session["ChosenNotes"] = data["Notes"];
                        Session["ChosenSysSerial"] = data["SysSerial"];
                        Session["ChosenSysSerial"] = data["SysSerial"];
                        Session["ChosenItemCode"] = data["ItemCode"];
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}